package com.itheima.protocol.json;

import com.alibaba.fastjson.JSON;
import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.itheima.protocol.Person;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class jsonDeno {

    public static void main(String[] args) throws Exception {

        executeJson();
        executeFastJson();
        executeJProtobuf();
        executeHessian();
    }

    private static Person init() {

        Person person = new Person();
        person.setName("Mic");
        person.setAge(20);

        return person;
    }

    private static void executeJson() {
        byte[] writeBytes = null;
        Person person = init();
        ObjectMapper mapper = new ObjectMapper();

        Long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {

            try {
                writeBytes = mapper.writeValueAsBytes(person);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Json序列化:" + (System.currentTimeMillis() - start) + "ms" + " 总大小:" + writeBytes.length);

        //反序列化
        try {
            Person value = mapper.readValue(writeBytes, Person.class);
            System.out.println(value);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void executeFastJson() {
        Person person = init();
        Long start = System.currentTimeMillis();

        String text = null;
        for (int i = 0; i < 100; i++) {
            text = JSON.toJSONString(person);
        }
        System.out.println("FastJson序列化:" + (System.currentTimeMillis() - start) + "ms" +
                " 总大小:" + text.getBytes().length);

        //反序列化
        Person object = JSON.parseObject(text, Person.class);
        System.out.println(object);
    }

    private static void executeJProtobuf() throws Exception {
        Person person = init();
        Codec<Person> personCodec = ProtobufProxy.create(Person.class, false);
        Long start = System.currentTimeMillis();
        byte[] encode = null;
        for (int i = 0; i < 100; i++) {
            encode = personCodec.encode(person);
        }
        System.out.println("JProtobuf序列化:" + (System.currentTimeMillis() - start) + "ms" +
                " 总大小:" + encode.length);

        //反序列化
        Person decode = personCodec.decode(encode);
        System.out.println(decode);
    }

    private static void executeHessian() throws Exception {
        Person person = init();

        Long start = System.currentTimeMillis();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        HessianOutput ho = new HessianOutput(bos);

        for (int i = 0; i < 100; i++) {
            ho.writeObject(person);
            if(i == 0){
                System.out.println("第一次的结果:"+bos.toByteArray().length);
            }
        }
        System.out.println("Hessian序列化:" + (System.currentTimeMillis() - start) + "ms" +
                " 总大小:" + bos.toByteArray().length);

        //反序列化
        HessianInput hi = new HessianInput(new ByteArrayInputStream(bos.toByteArray()));
        Person person1 = (Person)hi.readObject();
        System.out.println(person1);
    }
}
